
package CSS::Preprocessor::Expression::If;

use base qw(CSS::Preprocessor::Expression);
use strict;
use warnings;
use constant FIELDS => [qw(
    if_cond
    if_block
    elseif_conds
    elseif_blocks
    else_block
)
];
use fields @{&FIELDS};

use Carp;

sub new {
    my ($class, %args) = @_;

    my $self = $class->SUPER::new();

    foreach my $k (@{&FIELDS}) {
        $self->{$k} = delete $args{$k};
    }

    croak "Unrecognised arguments ".join(',',keys %args) if %args;

    return $self;
}

sub _as_string_inner {
    my $self = shift;

    my $ret = "";

    $ret .= "if (".$self->{if_cond}.") ".$self->{if_block};

    if (@$self->{elseif_conds}) {
        for (my $i = 0; $i <= $#{$self->{elseif_conds}}; $i++) {
            $ret .= " elseif (".$self->{elseif_conds}[$i].") ".$self->{elseif_blocks}[$i];
        }
    }

    if ($self->{else_block}) {
        $ret .= " else ".$self->{else_block};
    }

    return $ret;
}

=head1 NAME

CSS::Preprocessor::Expression::If - Class representing an @-if expression

=cut

1;

